﻿var marker_vnpt_site_load_view;
var vnpt_site_lat_load_view;
var vnpt_site_lng_load_view;
var latLng_vnpt_site_load_view;
var bounds_vnpt_site_load_view;
var imageUrl_vnpt_site_load_view;
var markers_array_vnpt_site_load_view = [];
var infowindow_vnpt_site_load_view = new google.maps.InfoWindow();

function close_window_elements_popupon_maps() {



    try {

        infowindow_vnpt_site_load_view.close();
    }
    catch (exception) { }


}

function VIEW_VNPT_SITE_ON_MAPS() {


    $(".Maps_pageInDex").mask("Dữ liệu đang được xử lý ...");

    var StringGetLopBanDo = "";

    var valLopBanDo = $('#my_selct_loai_ban_do_find_vnpt_site').combotree('getValues');

    if (valLopBanDo.length == 0) {
        StringGetLopBanDo = "";
    }
    else {
        StringGetLopBanDo = valLopBanDo.join(",");
    }

    setTimeout(function () {

        $.ajax({ url: _nameSubDomain + "/BanDo/ViewSiteOnMaps?lopbando=" + StringGetLopBanDo, async: false }).done(function (data) {

            while (markers_array_vnpt_site_load_view.length) {
                 markers_array_vnpt_site_load_view.pop().setMap(null);
            }

            var vnpt_site = eval(data);

            for (var xy_vnpt_site = 0; xy_vnpt_site < vnpt_site.length; xy_vnpt_site++) {

                if ((vnpt_site[xy_vnpt_site]._entity.Y != "" || vnpt_site[xy_vnpt_site]._entity.Y != null) && (vnpt_site[xy_vnpt_site]._entity.X != "" || vnpt_site[xy_vnpt_site]._entity.X != null)) {

                    imageUrl_vnpt_site_load_view = domain_url + _nameSubDomain + "/Content/fileUpload/" + vnpt_site[xy_vnpt_site].images;


                    marker_vnpt_site_load_view = new google.maps.MarkerImage(imageUrl_vnpt_site_load_view,
                        new google.maps.Size(48, 48));

                    vnpt_site_lat_load_view = parseFloat(vnpt_site[xy_vnpt_site]._entity.Y);
                    vnpt_site_lng_load_view = parseFloat(vnpt_site[xy_vnpt_site]._entity.X);

                    latLng_vnpt_site_load_view = new google.maps.LatLng(vnpt_site_lat_load_view, vnpt_site_lng_load_view);

                    //bounds_vnpt_site_load_view.extend(latLng_vnpt_site_load_view);


                    marker_vnpt_site_load_view = new MarkerWithLabel({
                        position: latLng_vnpt_site_load_view,
                        map: _mapsMain,
                        draggable: false,
                        raiseOnDrag: true,
                        crossOnDrag: true,
                        icon: imageUrl_vnpt_site_load_view,
                        labelContent: vnpt_site[xy_vnpt_site]._entity.TEN,
                        labelAnchor: new google.maps.Point(100, 0),
                        labelClass: "labels_vnpt", // the CSS class for the label
                        labelStyle: { opacity: 0.75 },
                        id: vnpt_site[xy_vnpt_site]._entity.SITEID

                    });


                    //market_vnpt_site_load_view.mycategory = vnpt_site[xy_vnpt_site]["loaisite"];


                    markers_array_vnpt_site_load_view.push(marker_vnpt_site_load_view);


                    google.maps.event.addListener(marker_vnpt_site_load_view, 'rightclick', (function (marker_vnpt_site_load_view, xy_vnpt_site) {
                        return function () {
                            try {

                                infowindow_vnpt_site_load_view.close();
                            }
                            catch (exception) { }
                            finally {

                                var myOptions = {
                                    draggable: true
                                };

                                marker_vnpt_site_load_view.setOptions(myOptions);

                            }
                        }

                    })(marker_vnpt_site_load_view, xy_vnpt_site));

                    google.maps.event.addListener(marker_vnpt_site_load_view, 'dblclick', (function (marker_vnpt_site_load_view, xy_vnpt_site) {
                        return function () {
                            try {
                                infowindow_vnpt_site_load_view.close();
                            }
                            catch (exception) { }
                            finally {

                                var vnpt_site_lat_load_info_dbclick = parseFloat(vnpt_site[xy_vnpt_site]._entity.Y);
                                var vnpt_site_lng_load_info_dbclick = parseFloat(vnpt_site[xy_vnpt_site]._entity.X);
                                var lat_long_view_load_infor_dbclick = new google.maps.LatLng(vnpt_site_lat_load_info_dbclick, vnpt_site_lng_load_info_dbclick);
                                

                                $.ajax({
                                    url: _nameSubDomain + '/Vnpt_Site/UpdateVNPTSITE_On_Maps?SiteId=' + vnpt_site[xy_vnpt_site]._entity.SITEID + "&x=" + vnpt_site_lng_load_info_dbclick + "&y=" + vnpt_site_lat_load_info_dbclick,
                                    success: function (data) {
                                        infowindow_vnpt_site_load_view.setContent(data);
                                        infowindow_vnpt_site_load_view.open(_mapsMain);
                                        infowindow_vnpt_site_load_view.setPosition(lat_long_view_load_infor_dbclick);
                                        google.maps.event.addListener(infowindow_vnpt_site_load_view, 'domready', function () {
                                            // whatever you want to do once the DOM is ready
                                            LoadJavascripit_UpDateVnPTSITE();

                                        });
                                    }
                                });

                            }
                        }
                    })(marker_vnpt_site_load_view, xy_vnpt_site));


                    google.maps.event.addListener(marker_vnpt_site_load_view, 'dragend', (function (marker_vnpt_site_load_view, xy_vnpt_site) {
                        return function () {
                            try {
                                infowindow_vnpt_site_load_view.close();
                            }
                            catch (exception) { }
                            finally {

                                var point = marker_vnpt_site_load_view.getPosition();
                                var vnpt_site_lat_load_info = parseFloat(point.lat());
                                var vnpt_site_lng_load_info = parseFloat(point.lng());
                                var lat_long_view_load_infor = new google.maps.LatLng(vnpt_site_lat_load_info, vnpt_site_lng_load_info);
                                
                                $.ajax({
                                    url: _nameSubDomain + '/Vnpt_Site/UpdateVNPTSITE_On_Maps?SiteId=' + vnpt_site[xy_vnpt_site]._entity.SITEID + "&x=" + vnpt_site_lng_load_info + "&y=" + vnpt_site_lat_load_info,
                                    success: function (data) {
                                        infowindow_vnpt_site_load_view.setContent(data);
                                        infowindow_vnpt_site_load_view.open(_mapsMain);
                                        infowindow_vnpt_site_load_view.setPosition(lat_long_view_load_infor);
                                        google.maps.event.addListener(infowindow_vnpt_site_load_view, 'domready', function () {
                                            // whatever you want to do once the DOM is ready
                                            LoadJavascripit_UpDateVnPTSITE();

                                        });
                                    }
                                });
                            }
                        }
                    })(marker_vnpt_site_load_view, xy_vnpt_site));


                }
            }

            setTimeout(function () { $(".Maps_pageInDex").unmask(); }, 1000);

        });

    }, 100);

}

function reload_vn_pt_site_when_update(id) {

    var IdText = parseInt(id);

    for (var i = 0; i < markers_array_vnpt_site_load_view.length; i++) {
        if (markers_array_vnpt_site_load_view[i].id == IdText) {
            markers_array_vnpt_site_load_view[i].setMap(null);
            markers_array_vnpt_site_load_view.splice(i, 1);
        }
    }

    var bounds_load_view_id = new google.maps.LatLngBounds();

    $.ajax({ url: _nameSubDomain + "/Vnpt_Site/getString_VNPTSITEById?Id=" + id, async: false }).done(function (data) {

        var vnpt_site = data;

        var get_sotr = 0;

        imageUrl_vnpt_site_load_view = domain_url + _nameSubDomain + "/Content/fileUpload/" + vnpt_site.images;


        markerImage_load_view = new google.maps.MarkerImage(imageUrl_vnpt_site_load_view,
            new google.maps.Size(48, 48));

        var vnpt_site_lat_id = parseFloat(vnpt_site._entity.Y);
        var vnpt_site_lng_id = parseFloat(vnpt_site._entity.X);

        var latLng_load_view = new google.maps.LatLng(vnpt_site_lat_id, vnpt_site_lng_id);

        bounds_load_view_id.extend(latLng_load_view);

        marker_vnpt_site_load_view = new MarkerWithLabel({
            position: latLng_load_view,
            map: _mapsMain,
            draggable: false,
            raiseOnDrag: true,
            crossOnDrag: true,
            icon: imageUrl_vnpt_site_load_view,
            labelContent: vnpt_site._entity.TEN,
            labelAnchor: new google.maps.Point(100, 0),
            labelClass: "labels_vnpt", // the CSS class for the label
            labelStyle: { opacity: 0.75 },
            id: vnpt_site._entity.SITEID

        });

        markers_array_vnpt_site_load_view.push(marker_vnpt_site_load_view);


        google.maps.event.addListener(marker_vnpt_site_load_view, 'rightclick', (function (marker_vnpt_site_load_view, get_sotr) {
            return function () {
                try {
                    infowindow_vnpt_site_load_view.close();
                }
                catch (exception) { }
                finally {


                    var myOptions = {
                        draggable: true
                    };

                    marker_vnpt_site_load_view.setOptions(myOptions);

                }
            }

        })(marker_vnpt_site_load_view, get_sotr));

        google.maps.event.addListener(marker_vnpt_site_load_view, 'dblclick', (function (marker_vnpt_site_load_view, get_sotr) {
            return function () {
                try {
                    infowindow_vnpt_site_load_view.close();
                }
                catch (exception) { }
                finally {
                    
                    var vnpt_site_lat_load_info_dbclick = parseFloat(vnpt_site._entity.Y);
                    var vnpt_site_lng_load_info_dbclick = parseFloat(vnpt_site._entity.X);
                    var lat_long_view_load_infor_dbclick = new google.maps.LatLng(vnpt_site_lat_load_info_dbclick, vnpt_site_lng_load_info_dbclick);


                    $.ajax({
                        url: _nameSubDomain + '/Vnpt_Site/UpdateVNPTSITE_On_Maps?SiteId=' + vnpt_site._entity.SITEID + "&x=" + vnpt_site_lng_load_info_dbclick + "&y=" + vnpt_site_lat_load_info_dbclick,
                        success: function (data) {
                            infowindow_vnpt_site_load_view.setContent(data);
                            infowindow_vnpt_site_load_view.open(_mapsMain);
                            infowindow_vnpt_site_load_view.setPosition(lat_long_view_load_infor_dbclick);
                            google.maps.event.addListener(infowindow_vnpt_site_load_view, 'domready', function () {
                                // whatever you want to do once the DOM is ready
                                LoadJavascripit_UpDateVnPTSITE();

                            });
                        }
                    });

                }
            }
        })(marker_vnpt_site_load_view, get_sotr));


        google.maps.event.addListener(marker_vnpt_site_load_view, 'dragend', (function (marker_vnpt_site_load_view, get_sotr) {
            return function () {
                try {

                    infowindow_vnpt_site_load_view.close();
                }
                catch (exception) { }
                finally {
                    
                    var point = marker_vnpt_site_load_view.getPosition();
                    var vnpt_site_lat_load_info = parseFloat(point.lat());
                    var vnpt_site_lng_load_info = parseFloat(point.lng());
                    var lat_long_view_load_infor = new google.maps.LatLng(vnpt_site_lat_load_info, vnpt_site_lng_load_info);

                    $.ajax({
                        url: _nameSubDomain + '/Vnpt_Site/UpdateVNPTSITE_On_Maps?SiteId=' + vnpt_site._entity.SITEID + "&x=" + vnpt_site_lng_load_info + "&y=" + vnpt_site_lat_load_info,
                        success: function (data) {
                            infowindow_vnpt_site_load_view.setContent(data);
                            infowindow_vnpt_site_load_view.open(_mapsMain);
                            infowindow_vnpt_site_load_view.setPosition(lat_long_view_load_infor);
                            google.maps.event.addListener(infowindow_vnpt_site_load_view, 'domready', function () {
                                // whatever you want to do once the DOM is ready
                                LoadJavascripit_UpDateVnPTSITE();

                            });
                        }
                    });

                }
            }
        })(marker_vnpt_site_load_view, get_sotr));

        //_mapsMain.initialZoom = true;
        //_mapsMain.fitBounds(bounds_load_view_id);
        //_mapsMain.setZoom(13);

    });

}

function LoadJavascripit_UpDateVnPTSITE() {

    

    $("#COM_LOAISITE_ADDVTSITE_MAPS").combobox({
        url: _nameSubDomain + "/Vnpt_Site/listJsonIntoCateTree",
        valueField: "MA",
        textField: "TEN",
        onSelect: function (row) {
            $("#LOAISITE_ADDVNPTSITE").val(row.MA);
        }

    });

    $("#COM_DONVIQL_ADDVTSITE_MAPS").combobox({

        url: _nameSubDomain + "/Donvi_ToQL/Get_DonVi_To_OnJson_InsertText",
        valueField: "id",
        textField: "text",
        onSelect: function (row) {

            $("#DONVIQL_ADDVNPTSITE_MAPS").val(row.id);
            $("#COM_TOQL_ADDVTSITE_MAPS").combobox({ url: _nameSubDomain + "/Donvi_ToQL/Get_ToAL_To_OnJson_InsertText?ma=" + row.id });
            $("#TOQL_ADDVNPTSITE_MAPS").val("");

        }

    });

    $("#COM_TOQL_ADDVTSITE_MAPS").combobox({
        url: _nameSubDomain + "/Donvi_ToQL/Get_ToAL_To_OnJson_InsertText?ma=" + $("#DONVIQL_ADDVNPTSITE_MAPS").val(),
        valueField: "MA",
        textField: "TEN",
        onSelect: function (row) {
            $("#TOQL_ADDVNPTSITE_MAPS").val(row.MA);
            $("#COM_QUANHUYEN_ADDVTSITE_MAPS").combobox({ url: _nameSubDomain + "/Vnpt_Site/ListQuanHuyenbyTo?toql=" + row.MA });
        },

    });

    $("#COM_QUANHUYEN_ADDVTSITE_MAPS").combobox({

        url: _nameSubDomain + "/Vnpt_Site/ListQuanHuyen?toql=" + $("#TOQL_ADDVNPTSITE_MAPS").val(),
        valueField: "MA_QUAN",
        textField: "TEN_QUAN",
        onSelect: function (row) {

            $("#QUANHUYEN_ADDVNPTSITE_MAPS").val(row.MA_QUAN);
            $("#COM_PHUONGXA_ADDVTSITE_MAPS").combobox({ url: _nameSubDomain + "/Vnpt_Site/ListPhuongXa?quanhuyenId=" + row.MA_QUAN + "&toql=" + $("#TOQL_ADDVNPTSITE_MAPS").val() });
            $("#PHUONGXA_ADDVNPTSITE_MAPS").val("");

        }

    });

    $("#COM_PHUONGXA_ADDVTSITE_MAPS").combobox({
        url: _nameSubDomain + "/Vnpt_Site/ListPhuongXa?quanhuyenId=" + $("#QUANHUYEN_ADDVNPTSITE_MAPS").val() + "&toql=" + $("#TOQL_ADDVNPTSITE_MAPS").val(),
        valueField: "MA_PHUONG",
        textField: "TEN_PHUONG",
        onSelect: function (row) {
            $("#PHUONGXA_ADDVNPTSITE_MAPS").val(row.MA_PHUONG);
        },

    });

}

function opBeginProcess_loading_window_submit_MAPS() {
    $("#ContentwindowFunction_maps").mask("Dữ liệu đang xử lý");
}

function completedAjaxRequestmADVTPNSITE_maps(obj) {

    var returnObj = eval('(' + obj.responseText + ')');

    if (returnObj.Status == "success") {
        
        reload_vn_pt_site_when_update($("#SITEID_ADDVNPTSITE_MAPS").val());

        try {

            infowindow_vnpt_site_load_view.close();
        }
        catch (exception) { }


    } else {

        $("#ContentwindowFunction_maps").unmask();
        confirmOptionNormal(returnObj.Message);
    }

    return false;

}


// add site on maps
var market_vnpt_site_add_Drawing;
var market_vnpt_site_add_Drawing_Array = [];
var _new_vnpt_site_main_maps;

function Add_vnpt_site_maps() {

    try {
        market_vnpt_site_add_Drawing.setDrawingMode(null);
    }
    catch (e) { }

    var option_vnpt_site = {
        icon: _nameSubDomain + '/Content/images/_marker_greenA.png',
        editable: true
    };


    market_vnpt_site_add_Drawing = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.MARKER,
        drawingControl: false,
        markerOptions: option_vnpt_site,
        map: _mapsMain
    });

    google.maps.event.addListener(market_vnpt_site_add_Drawing, 'overlaycomplete', function (e) {

        if (e.type == google.maps.drawing.OverlayType.MARKER) {

            market_vnpt_site_add_Drawing.setDrawingMode(null);

            _new_vnpt_site_main_maps = e.overlay;
            _new_vnpt_site_main_maps.type = e.type;

            market_vnpt_site_add_Drawing_Array.push(_new_vnpt_site_main_maps);

            var _marker_center_point_main_map = _new_vnpt_site_main_maps.getPosition();
            _marker_center_point_main_map_Lat = _marker_center_point_main_map.lat();
            _marker_center_point_main_map_Lng = _marker_center_point_main_map.lng();

            var _line_latLng_main_vnpt_site_map_ = new google.maps.LatLng(_marker_center_point_main_map_Lat, _marker_center_point_main_map_Lng);

            try {
                infowindow_vnpt_site_load_view.close();
            }
            catch (exception) { }
            finally {


                $.ajax({
                    url: _nameSubDomain + '/Vnpt_Site/AddVNPTSITE_On_Maps?x=' + _marker_center_point_main_map_Lng + "&y=" + _marker_center_point_main_map_Lat,
                    success: function (data) {
                        infowindow_vnpt_site_load_view.setContent(data);
                        infowindow_vnpt_site_load_view.open(_mapsMain);
                        infowindow_vnpt_site_load_view.setPosition(_line_latLng_main_vnpt_site_map_);
                        google.maps.event.addListener(infowindow_vnpt_site_load_view, 'domready', function () {
                            // whatever you want to do once the DOM is ready
                            loadScriptWindownAddSite();
                        });
                    }
                });

            }
           

            ////////////Add Event Click For Shape
            var listener = google.maps.event.addListener(_new_vnpt_site_main_maps, 'click', function (event) {
                /////////////set thong tin moi
                ////////////////// show ra
                try {
                    infowindow_vnpt_site_load_view.close();
                }
                catch (exception) { }
                finally {


                    $.ajax({
                        url: _nameSubDomain + '/Vnpt_Site/AddVNPTSITE_On_Maps?x=' + _marker_center_point_main_map_Lng + "&y=" + _marker_center_point_main_map_Lat,
                        success: function (data) {
                            infowindow_vnpt_site_load_view.setContent(data);
                            infowindow_vnpt_site_load_view.open(_mapsMain);
                            infowindow_vnpt_site_load_view.setPosition(event.latLng);
                            google.maps.event.addListener(infowindow_vnpt_site_load_view, 'domready', function () {
                                // whatever you want to do once the DOM is ready
                                loadScriptWindownAddSite();
                            });
                        }
                    });

                }


            });

        }
    });


}

function close_window_elements_add_vnpt_site() {

    try {
        market_vnpt_site_add_Drawing.setDrawingMode(null);
        market_vnpt_site_add_Drawing.setMap(null);
        _new_vnpt_site_main_maps.setMap(null);
    }
    catch (e) { }

    try {
        infowindow_vnpt_site_load_view.close();
    }
    catch (exception) { }

}

function loadScriptWindownAddSite() {

    $("#COM_LOAISITE_ADDVTSITE_add_vnpt_site_ma").combobox({
        url: _nameSubDomain + "/Vnpt_Site/listJsonIntoCateTree",
        valueField: "MA",
        textField: "TEN",
        onSelect: function (row) {
            $("#LOAISITE_ADDVNPTSITE_add_vnpt_site_ma").val(row.MA);
        }

    });


    $("#COM_DONVIQL_ADDVTSITE_add_vnpt_site_ma").combobox({

        url: _nameSubDomain + "/Donvi_ToQL/Get_DonVi_To_OnJson_InsertText",
        valueField: "id",
        textField: "text",
        onSelect: function (row) {

            $("#DONVIQL_ADDVNPTSITE_add_vnpt_site_ma").val(row.id);
            $("#COM_TOQL_ADDVTSITE_add_vnpt_site_ma").combobox({ url: _nameSubDomain + "/Donvi_ToQL/Get_ToAL_To_OnJson_InsertText?ma=" + row.id });
            $("#TOQL_ADDVNPTSITE_add_vnpt_site_ma").val("");

        }

    });

    $("#COM_TOQL_ADDVTSITE_add_vnpt_site_ma").combobox({

        url: _nameSubDomain + "/Donvi_ToQL/Get_ToAL_To_OnJson_InsertText?ma=",
        valueField: "MA",
        textField: "TEN",
        onSelect: function (row) {
            $("#TOQL_ADDVNPTSITE_add_vnpt_site_ma").val(row.MA);
            $("#COM_QUANHUYEN_ADDVTSITE_add_vnpt_site_ma").combobox({ url: _nameSubDomain + "/Vnpt_Site/ListQuanHuyenbyTo?toql=" + row.MA });
        },

    });

    $("#COM_QUANHUYEN_ADDVTSITE_add_vnpt_site_ma").combobox({

        //url: _nameSubDomain + "/Vnpt_Site/ListQuanHuyen",
        valueField: "MA_QUAN",
        textField: "TEN_QUAN",
        onSelect: function (row) {

            $("#QUANHUYEN_ADDVNPTSITE_add_vnpt_site_ma").val(row.MA_QUAN);
            $("#COM_PHUONGXA_ADDVTSITE_add_vnpt_site_ma").combobox({ url: _nameSubDomain + "/Vnpt_Site/ListPhuongXa?quanhuyenId=" + row.MA_QUAN + "&toql=" + $("#TOQL_ADDVNPTSITE_add_vnpt_site_ma").val() });
            $("#PHUONGXA_ADDVNPTSITE_add_vnpt_site_ma").val("");

        }

    });

    $("#COM_PHUONGXA_ADDVTSITE_add_vnpt_site_ma").combobox({

        valueField: "MA_PHUONG",
        textField: "TEN_PHUONG",
        onSelect: function (row) {
            $("#PHUONGXA_ADDVNPTSITE_add_vnpt_site_ma").val(row.MA_PHUONG);
        },

    });

}

function opBeginProcess_loading_window_submit_add_vnpt_site_maps() {
    $("#ContentwindowFunction_add_vnpt_site_maps").mask("Dữ liệu đang xử lý");
}

function completedAjaxRequest_add_vnpt_maps(obj) {

    var returnObj = eval('(' + obj.responseText + ')');

    if (returnObj.Status == "success") {

        reload_vn_pt_site_when_update(returnObj.RedirectUrl);

        try {
            market_vnpt_site_add_Drawing.setDrawingMode(null);
            market_vnpt_site_add_Drawing.setMap(null);
            _new_vnpt_site_main_maps.setMap(null);
        }
        catch (e) { }

        try {
            infowindow_vnpt_site_load_view.close();
        }
        catch (exception) { }


    } else {

        $("#ContentwindowFunction_add_vnpt_site_maps").unmask();
        confirmOptionNormal(returnObj.Message);
    }

    return false;

}

function remove_vnpt_site_maps(id, siteName) {

    $.confirm({
        text: "Bạn chắc chắn muốn xóa vnpt site </b>"  + siteName + "</b>",
        confirm: function (button) {

            $("#ContentwindowFunction_maps").mask("Dữ liệu đang xử lý");

            $.ajax({ url: _nameSubDomain + "/Vnpt_Site/_DeleteVNPTSITE_MAPS?SITEID=" + id, async: false }).done(function (data) {

                for (var i = 0; i < markers_array_vnpt_site_load_view.length; i++) {
                    if (markers_array_vnpt_site_load_view[i].id == id) {
                        markers_array_vnpt_site_load_view[i].setMap(null);
                        markers_array_vnpt_site_load_view.splice(i, 1);
                    }
                }

                try {
                    infowindow_vnpt_site_load_view.close();
                }
                catch (exception) { }

            });

        },
        cancel: function (button) {

        }
    });


    return false;
    


}